<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="所属银行" prop="bank_name">
        <el-select v-model="dataForm.bank_name" @change="pitchOn" placeholder="请选择">
          <el-option v-for="item in options" :key="item.id" :label="item.name" :value="{name:item.name,icon:item.icon}">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="银行卡号" prop="card_num">
        <el-input v-model="dataForm.card_num" maxlength="19" onkeyup="value=value.replace(/^(0+)|[^\d]+/g,'')"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="dataForm.name" maxlength="5"></el-input>
      </el-form-item>
      <el-form-item label="预留手机号" prop="mobile">
        <el-input v-model="dataForm.mobile" maxlength="11" onkeyup="value=value.replace(/^(0+)|[^\d]+/g,'')">
        </el-input>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  treeDataTranslate
} from '@/utils'
import { isEmail, isMobile } from '@/utils/validate'
import seviceApi from '@/api/serviceApi.js'
export default {
  data () {
    var validateMobile = (rule, value, callback) => {
      if (!isMobile(value)) {
        callback(new Error('手机号格式错误'))
      } else {
        callback()
      }
    }
    var validateCardNum = (rule, value, callback) => {
      if (value.length < 16 || value.length > 19) {
        callback(new Error('银行卡号长度必须在16到19之间'))
      } else {
        callback()
      }
    }
    return {
      visible: false,
      dataForm: {
        id: '0',
        icon: '',
        bank_name: '',
        card_num: '',
        identity_card: '',
        name: '',
        mobile: ''
      },
      options: [],
      dataRule: {
        bank_name: [{
          required: true,
          message: '请选择银行卡',
          trigger: 'blur'
        }],
        card_num: [{
          required: true,
          message: '银行账号不能为空',
          trigger: 'blur'
        },
        { validator: validateCardNum, trigger: 'blur' }
        ],
        name: [{
          required: true,
          message: '姓名不能为空',
          trigger: 'blur'
        }],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { validator: validateMobile, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
      })
      // 获取银行卡名称列表
      seviceApi.getBankcardList().then(res => {
        if (res && res.data.code == 200) {
          this.options = res.data.list
        }
      })
      if (id != undefined) {
        seviceApi.getBankCardInfo(id).then(res => {
          if (res && res.data.code == 200) {
            this.dataForm = res.data.data
          }
        })
      }
    },
    pitchOn (e) {
      this.dataForm.icon = e.icon
      this.dataForm.bank_name = e.name
    },
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          let hint = ''
          if (this.dataForm.id == 0) {
            delete this.dataForm.id
            hint = '添加'
          } else {
            this.dataForm.mark_code = this.markCode
            hint = '修改'
          }
          seviceApi.saveBankCard(this.dataForm).then(res => {
            if (res && res.data.code == 200) {
              this.$message({
                message: hint + '成功',
                type: 'success'
              })
              this.visible = false
              this.$emit('updataList', 1)
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss">
</style>
